package xjtu.zd.server.book.detail;

import org.springframework.web.bind.annotation.*;
import xjtu.zd.server.common.CommonService;
import xjtu.zd.server.common.CurrentUser;
import xjtu.zd.server.common.Result;
import xjtu.zd.server.common.TokenUtils;

import javax.annotation.Resource;

@RestController
@RequestMapping("/book/detail")
public class DetailAPI {

    //引入依赖对象
    @Resource
    private CommonService commonService;

    @Resource
    private DetailService detailService;

    @RequestMapping("/test")
    public String test() {
        return "this is a test!";
    }

    @RequestMapping("/info/{b_id}")
    public Result info(@PathVariable int b_id){
        return Result.success(detailService.getInfo(b_id));
    }
    @GetMapping("/getScore/{b_id}")
    public Result getScore(@PathVariable String b_id) {
        return Result.success(detailService.getScore(b_id));
    }
    @RequestMapping("/intro/{b_id}")
    public Result intro(@PathVariable int b_id){
        return Result.success(detailService.getInfo(b_id));
    }

    @RequestMapping("/setScore")
    public Result setScore(@RequestHeader("Token") String token, @RequestBody ScoDto dto){
        CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);

        dto.setU_id(currentUser.getUserId());
        detailService.setScore(dto);

        return Result.success();
        //http://localhost/info?b_id=1001
    }
}